
clear
clear all
clear mata
clear matrix
set more off
set matsize 11000
set maxvar 30000
cap log off
capture log close
set emptycells drop
pause on


***********************************************
* USER DEFINE FILEPATH FOR REPLICATION FOLDER *
***********************************************

global path "C:\Users\wb520443\Dropbox\Research Projects\Global pollution\2_analysis\Replication"



***********************************************


global opts		a f plain coll(none) nodep nomti c(b(star fmt(%9.3f)) se(abs par fmt(%9.3f))  ) star(* .10 ** .05 *** .01) noobs nocons

global opts_fs		a f plain coll(none) nodep nomti c(b(star fmt(%9.3f)) se(abs par fmt(%9.3f))) star(* .10 ** .05 *** .01) noobs nocons


global input "$path\data"
global figdat "$path\figdat"
global figures "$path\figures"
global tables "$path\tables"

	use "$input\rwi_pollution_analysis_collapsed.dta", replace
	
	replace error=1/error
	g both=population*error
	g both2=country_population*error
	
	encode country, g(country_code)
	
	preserve
		
		replace mean_pm25=100 if mean_pm25>100 & mean_pm25!=.
		g bin=ceil(mean_pm25/5)
			*replace bin=100 if bin>100 & bin!=.
			bysort bin: egen wtmean_rwi=wtmean(rwi), weight(both2)
			bysort bin: egen urca_mean=mean(urca)
				replace urca_mean=abs(30-urca_mean)
			duplicates drop bin, force
			g wealth=1+wtmean_rwi
			
			keep wtmean_rwi urca_mean bin wealth
			
			export delimited using "$figdat/binned_rwi.csv", replace
	
	restore